<?php
require_once 'am/grid/action/Export.php';
class DownloadController extends Am_Controller_GridUsercp
{
	public function preDispatch()
    {
        parent::preDispatch();
		//$this->getDi()->auth->requireLogin(ROOT_URL . '/member');
		
      
    }
    function indexAction()
    {
		$ds = new Am_Query_Report;
		$ds->innerJoin('?_job', 'j', " r.job_id_server = j.job_id_server");
		$ds->addField('j.job_id');
		
		
			
        
		
		$ds->addWhere("j.job_id = ?", $this->getParam('jobid'));
		$ds->setOrder("id");
		$grid = new Am_Grid_Editable('_r', ___("Browse Reports"), $ds, $this->_request, $this->view);
		
		$grid->addGridField(new Am_Grid_Field('id', 'ID', true, '', null, null));
		$grid->addGridField(new Am_Grid_Field('site', 'Site', true, '', null, null));
		
		
		
	
	
		$grid->addGridField(new Am_Grid_Field('url_link', 'URL Link', true, '', null, null));
		$grid->addGridField(new Am_Grid_Field('profile_page', 'Profile Page', true, '', null, null));
		$grid->addGridField(new Am_Grid_Field('vote', 'Vote', true, '', null, null));
		$grid->addGridField(new Am_Grid_Field('status', ___('Status'), true, '', null, null));
		$grid->addGridField(new Am_Grid_Field('date_time', ___('Time'), true, '', null, null));
		
		$grid->addGridField(new Am_Grid_Field('google_pr', 'Google PR', true, '', null, null));
		$grid->addGridField(new Am_Grid_Field('alexa_pr', 'Alexa PR', true, '', null, null));
		$grid->addGridField(new Am_Grid_Field('google_index', 'Google Index', true, '', null, null));
		$grid->addGridField(new Am_Grid_Field('last24hours', ___('Last 24 Hours'), true, '', null, null));
		$grid->addGridField(new Am_Grid_Field('lastweek', ___('Last Week'), true, '', null, null));
		$grid->addGridField(new Am_Grid_Field('lastmonth', 'Last Month', true, '', null, null));
		$grid->addGridField(new Am_Grid_Field('yahoo_link', 'Yahoo Link', true, '', null, null));
		$grid->addGridField(new Am_Grid_Field('submit_captcha', 'Submit Captcha', true, '', null, null));
	
		if($this->getParam('isuser')==0)
		$values = AM_Di::getInstance()->authAdmin->getUser()->getPref_Export($this->getPrefId($grid));
		else
		$values = AM_Di::getInstance()->auth->getUser()->getPref_Export($this->getPrefId($grid));
		
		$fields = array();
		foreach ($values as $fieldName) {
                $fields[$fieldName] = $this->getField($grid, $fieldName);
				
       }
		
		$values['jobid'] = $this->getParam("jobid");
		$export = new Am_Grid_Export_CSV();
		
		$export->run($grid, $grid->getDataSource($fields), $fields, $values);
		
		
    }
	 public function createGrid(){
	 	
	 }
	 protected function getPrefId($grid)
    {
        return 'grid_setup' . $grid->getId();
    }
	 public function getField($grid,$fieldName) {
        foreach ($grid->getFields() as $field)
            if ($field->getFieldName() == $fieldName)
                return $field;
        throw new Am_Exception_InternalError("Field [$fieldName] not found in " . __METHOD__);
    }
}